<!--
 * @Descripttion: 
 * @version: 
 * @Author: Connor
 * @Date: 2021-04-14 09:34:04
 * @LastEditors: Connor
 * @LastEditTime: 2021-04-14 20:07:39
-->
<template>
  <div>
      <el-row class="appraised-row">
          <el-link type="danger" @click="downloadTakeGoodWaybillModel()">下载导入模板</el-link>
      </el-row>

      <el-row class="appraised-row">
        <el-upload
          class="upload-demo"
          drag
          show-file-list="false"
          action="batchUploadTakeGoodWaybill"
          :http-request="uploadFn"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">先下载模板，根据模板填入数据再上传</div>
        </el-upload>
      </el-row>

      <el-row class="appraised-row"> 
          <el-col :span="2">导入结果：</el-col>
          <el-col :span="22">
               {{msg}}
          </el-col>
      </el-row>
  </div>
</template>
<script>
import { closeDialog } from "@/utils";
import axios from "axios";
import {ajax} from "@/store/yg/common/utils";
import * as constant from "@/store/yg/common/constant";
import { untransform } from '@/store/yg/common/uri'
export default {
 props: ["field","data"],
    data() {
      return {
          msg:"",
      };
     },
    mounted() {

    }, 
    methods: {
        downloadTakeGoodWaybillModel(){
          ajax("downloadTakeGoodWaybillModel",  {} ).then(
            res => {
            // console.log("res",res)
              if (res.path) {
                //helper.hide_loading();
                var fileName = res.path;
                if (fileName) {
                  window.open(constant.FILE_SERVER +fileName)
                } else {
                  this.$notify.error({
                    title: '错误',
                    message: '导出失败'
                  });
                }
              } else {
                this.$notify.error({
                    title: '错误',
                    message: res.errorMesage
                  });
              }
            },
            error => {
              console.log(error);
            }
          );
        },
        uploadFn(params){
          //自定义上传文件
          console.log(params)
          const file = params.file;
          
          // 根据后台需求数据格式
          const form = new FormData();
          // 文件对象
          form.append("items[]", file);
          axios({
            method: "post",
            url: constant.API_SERVER + "/api/consume/upload?uri="+untransform(this.$route.params.uri)+"&f=batchUploadTakeGoodWaybill&type=main",
            data: form
          }).then(response => {
            if (response.data.code === 200){
              //this.feeData.push({mes:response.data.data.fileName,fee:response.data.data.fullFileName});
              // this.$notify.success({
              //   title: '正确',
              //   message: '上传成功!'
              // });
               this.msg = '上传成功!'
               setTimeout(function(){
                   closeDialog();
                  location.reload();
               },3000);
              
            }else{
              this.msg = response.data.message
              // this.$notify.error({
              //   title: '错误',
              //   message: '上传失败!'+response.data.message
              // });
            }
          }).catch(error => {
            console.log(error);
            this.$notify.error({
              title: '错误',
              message: '上传失败!'
            });
          })
      },
    }
}
</script>
<style scoped>

</style>

<style lang="scss">
.row-item {
  color: #3f4652;
  line-height: 28px;
  margin-bottom: 14px;
}

  .appraised-row {
    text-align: left;
  }

  
</style>
